<?php

use Core\API_Controller;
use Models\User;

class RegisterController extends API_Controller
{
	public function index()
	{
		$this->load->library('form_validation');
			$this->form_validation->set_rules('phone', '用户名', 'required|is_unique[users.phone]')
			->set_rules('email', '邮箱', 'required|is_unique[users.email]')
			->set_rules('username', '姓名', 'required|max_length[32]')
			->set_rules('first_name', '姓', 'required|max_length[16]')
			->set_rules('last_name', '名', 'required|max_length[16]')
			->set_rules('password', '密码', 'required|min_length[6]');
			if ($this->form_validation->run()) {
				$user = new User();
				$user->load($this->input->post());
				$user['password'] = password_hash($this->input->post('password'), PASSWORD_DEFAULT);
				$user['create_at'] = date('Y-m-d H:i:s');
				$user->add();

				$this->session->authUser = (new User)->find($user['id']);

				// $access_token = $user->CreateSystemAccessToken();
				// return $this->response($access_token);
				return $this->success();
			} else {
				return $this->validateError($this->form_validation->error_array());
			}
	}
}